<template>
	<view class="">
		<view class="search-box pt-0 pb-0">
			<u-search placeholder="搜索商品" shape="square" v-model="searchData.name" @search="onSearch" :showAction="false" />
			<view class="custom-btn" @click.stop="$u.route('/pages/project/product/create')">新增商品</view>
		</view>
		<view class="my-list">
			<u-list :height="swiperHeight" @scrolltolower="scrolltolower" v-if="dataList.length > 0">
				<u-list-item v-for="(item, index) in dataList" :key="item.id">
					<view class="item fsize-26">
						<view class="flex align-center">
							<image class="width-140 height-140 border-radius-10 shrink0" :src="item.image"></image>
							<view class="w-100 ml-20">
								<view class="">{{item.name}}</view>
								<view class="mt-10 flex aliten-center">
									<view class="mr-10" v-if="item.brandText"><u-tag :text="item.brandText" plain size="mini" type="warning"></u-tag></view>
									<view v-if="item.goodsCategoryText"><u-tag :text="item.goodsCategoryText" plain size="mini" type="error"></u-tag></view>
								</view>
								<view class="mt-10 text-gray fsize-26">初始销售:{{item.initialSales}}</view>
								<view class="mt-10 text-gray fsize-26">销售价￥{{item.price}} · 成本价￥{{item.costprice}} · 市场价￥{{item.mktprice}}</view>
								<!-- <view class="mt-10 fsize-36 text-red"><text class="fsize-24">￥</text>{{item.price}}</view> -->
							</view>
						</view>
						<view class="mt-20 flex justify-between align-center border-t pt-20">
							<view class="flex align-center">
								<text class="mr-10">上架</text>
								<u-switch 
									v-model="item.isMarketable" 
									activeColor="#e54d42" 
									size="24" 
									inactiveColor="#606266"
									@change="(ev)=> {onSwitch(ev, item.id, 1)}"
								>
								</u-switch>
							</view>
							<view class="flex align-center">
								<text class="mr-10">推荐</text>
								<u-switch 
									v-model="item.isRecommend" 
									activeColor="#e54d42" 
									size="24" 
									inactiveColor="#606266"
									@change="(ev)=> {onSwitch(ev, item.id, 2)}"
								>
								</u-switch>
							</view>
							<view class="flex align-center">
								<text class="mr-10">热门</text>
								<u-switch 
									v-model="item.isHot" 
									activeColor="#e54d42" 
									size="24" 
									inactiveColor="#606266"
									@change="(ev)=> {onSwitch(ev, item.id, 3)}"
								>
								</u-switch>
							</view>
						</view>
						<view class="mt-20 flex justify-end align-center border-t pt-20">
							<view><u-button color="#2979ff" text="编辑" @click=""></u-button></view>
							<view class="ml-20"><u-button color="#e54d42" text="删除" @click="onDel(index)"></u-button></view>
						</view>
					</view>
				</u-list-item>
				<u-loadmore :status="loadmore.status" :icon-type="loadmore.iconType"
					:load-text="loadmore.loadText.loadText" margin-top="20" margin-bottom="20"
					class="coreshop-padding-top-10" />
			</u-list>
		</view>
		<!-- 无数据时默认显示 -->
		<view class="emptybox" v-if="dataList.length == 0">
			<u-empty icon-size="150" text="暂无数据" mode="data"></u-empty>
		</view>
	</view>
</template>

<script>
	import listHeightMixin from '@/common/computedAltitude.js'
	import share from '@/common/mixins/share.js'
	export default {
		mixins:[listHeightMixin,share],
		data() {
			return {
				loadmore: {
					status: 'loadmore',
					iconType: 'flower',
					loadText: {
						loadmore: '轻轻上拉',
						loading: '努力加载中',
						nomore: '实在没有了'
					},
				},
				dataList: [],
				brands: [],
				categoriesAll: [],
				searchData: {
					name: '',
					page: 1,
					limit: 99,
					share: 1,
				}
			}
		},
		onLoad() {
			this.getIndex()
			this.getPageList()
		},
		methods: {
			onDel(ix){
				const that = this
				uni.showModal({
					title: '提示',
					content: `是否删除【${that.dataList[ix].name}】吗?`,
					success: function (res) {
						if (res.confirm) {
							that.$reqJsonPost('CoreCmsGoodsDoDelete', {id: that.dataList[ix].id }).then(res=>{
								that.dataList.splice(ix, 1)
							})
						}
					}
				});
			},
			onSwitch(ev, id, tp){
				if(tp === 1){
					this.$reqJsonPost('CoreCmsGoodsDoSetisMarketable', {data: ev,id: id })
				}else if(tp===2){
					this.$reqJsonPost('CoreCmsGoodsDoSetisRecommend', {data: ev,id: id })
				}else if(tp===3){
					this.$reqJsonPost('CoreCmsGoodsDoSetisHot', {data: ev,id: id })
				}
			},
			// 搜索事件
			onSearch() {
				this.searchData.page = 1
				this.dataList = []
				this.getPageList()
			},
			getIndex(){
				this.$reqJsonPost('GoodsGetIndex').then(res=>{
					this.brands = res.data.brands
					this.categoriesAll = res.data.categoriesAll
				})
			},
			getPageList() {
				this.$reqPost('GetGoodsPageList', this.searchData).then(res => {
					if(res.data.length > 0){
						const upar1 = res.data.map(obj1 => {
							const matchingObj2 = this.brands.find(obj2 => obj2.id === obj1.brandId);
							if (matchingObj2) {
								obj1.brandText = matchingObj2.name;
							}else {
								obj1.brandText = '';
							}
							return obj1;
						});
						const upar2 = res.data.map(obj1 => {
							const matchingObj2 = this.categoriesAll.find(obj2 => obj2.id === obj1.goodsCategoryId);
							if (matchingObj2) {
								obj1.goodsCategoryText = matchingObj2.name;
							}else {
								obj1.goodsCategoryText = '';
							}
							return obj1;
						});
						this.dataList = [...this.dataList, ...upar2]
						if (res.count > this.dataList.length) {
							this.loadmore.status = 'loadmore'
							this.searchData.page++
						} else {
							// 数据已加载完毕
							this.loadmore.status = 'nomore'
						}
					}
				})
			},
			scrolltolower() {
				if (this.loadmore.status === 'loadmore') {
					this.getPageList()
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.img {
		width: 140rpx;
		height: 140rpx;
		border-radius: 10rpx;
	}
</style>
